﻿using EnvDTE80;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.TeamFoundation.Client;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TFhisto.Extension
{
    /// <summary>
    /// Interaction logic for MyControl.xaml
    /// </summary>
    public partial class TFhistoControl : UserControl
    {
        #region Fields
        private IList<VSITEMSELECTION> selectedItems;
        public IList<VSITEMSELECTION> SelectedItems
        {
            get { return selectedItems; }
            set { selectedItems = value; }
        }

        public string SelectedFile
        {
            set
            {
                ChangesetHistory1.AddSelectedFile(value);
            }
        }

        public string TeamProjectCollection
        {
            set
            {
                ChangesetHistory1.SelectedTeamProjectCollectionName = value;
            }
        }

        public string TeamProject
        {
            set
            {
                ChangesetHistory1.SelectedTeamProjectName = value;
            }
        }

        private DTE2 shellDTE;
        private static VersionControlExt versionControl;

        public DTE2 ShellDTE
        {
            set
            {
                shellDTE = value;
            }
        }
        #endregion

        public TFhistoControl()
        {
            InitializeComponent();
        }

        public void Refresh(string selectedFile)
        {
            versionControl = shellDTE.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as VersionControlExt;
            if (versionControl != null)
            {
                bool isInitialized = CommonUIHelper.Helper.IsInitialized();
                
                Workspace workspace = versionControl.SolutionWorkspace;
                TeamProject project = workspace.GetTeamProjectForLocalPath(shellDTE.Solution.FullName);

                if (!isInitialized)
                {
                    CommonUIHelper.Helper.Initialize(project.TeamProjectCollection);
                }

                TeamProjectCollection = project.TeamProjectCollection.Name;
                TeamProject = project.Name;
                SelectedFile = selectedFile;

                if (!isInitialized)
                {
                    ChangesetHistory1.LoadProject();
                }
            }
        }
    }
}